package cn.com.forever.LinkedList;

import java.util.ArrayList;

/*
    输入一个链表的头节点，按链表从尾到头的顺序返回每个节点的值（用数组返回）。
 */
public class PrintListFromTailToHead {
    public static void main(String[] args) {

    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode == null){
            return new ArrayList<>(0);
        }
        ArrayList<Integer> list = new ArrayList<>();
        ListNode next = null;
        ListNode head = null;
        while(listNode != null){
            next = listNode.next;
            listNode.next = head;
            head = listNode;
            listNode = next;
        }
        while(head != null){
            list.add(head.val);
            head = head.next;
        }
        return list;
    }
}
